<?php namespace Common\Model;
use Hdphp\Model\Model;

class Login extends Model{
    protected $table = 'user';

    protected $auto = [
        ['userpassword','md5','function',3,3]
    ];

    protected $validate = [
        ['username','required','用户账号必填',3,3],
        ['userpassword','required','密码必填',3,3],
        ['code','required','验证码必填',3,3],
        ['code','checkcode','验证码不正确',3,3],
        ['username','checkusername','用户账号不存在',3,3],
        ['userpassword','checkpassword','密码输入不正确',3,3]
    ];

    //    验证验证码
    public function checkcode(){
        $code=Q('post.code');
        if(strtolower($code)!=strtolower($_SESSION['code'])){
            return false;
        }else{
            return true;
        }
    }

    //    检测用户账号
    public function checkusername(){
        $username=Q('post.username');
        $name=$this->where("username='{$username}'")->get();
        if(!$name){
            return false;
        }else{
            return true;
        }
    }

    //            再判断密码是否正确

    public function checkpassword(){
        $username=Q('post.username');
        $name=$this->where("username='{$username}'")->get();
        $userpassword=Q('post.userpassword','','md5');
        if($userpassword!=$name[0]['userpassword']){
            return false;
        }else{
            return true;
        }

    }

    public function Login(){
        if(!$this->create()){
            return false;
        }else{
            $username=Q('post.username');
            $name=$this->where("username='{$username}'")->get();
            $_SESSION['userinfo']=[
                'username'=>$name[0]['usernickname'],
                'uid'=>$name[0]['uid']
            ];
            return true;
        }
    }
}